home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / BootingGallery.sit / Booting Gallery / Booting Gallery (source) / iconhack Folder / IconCatcher.c < prev    next >
C/C++ Source or Header  |  1996-06-22  |  10KB  |  477 lines

  1. #include <Shutdown.h>
  2. #include <Icons.h>
  3. #include <Folders.h>
  4. #include <Gestalt.h>
  5.  
  6. #pragma macsbug off
  7.  
  8. //#include <SetUpA4.h>
  9. //#include <A4Stuff.h>
  10.     long SetCurrentA4(void);
  11.     long GetCurrentA4(void) = 0x200C;
  12.     void Flush68KCodeCaches(void) = 0xA0BD;
  13.  
  14. #include "Blitter.h"
  15.  
  16. extern struct Keyboard {
  17.     short    wasted;
  18.     short    wasted2;
  19.     short    wasted3;
  20.     int command : 1;
  21.     int : 5;
  22.     int space : 1;
  23.     int tab : 1;
  24.     int : 4;
  25.     int control : 1;
  26.     int option : 1;
  27.     int capslock : 1;
  28.     int shift : 1;
  29. } keymap : 0x174;
  30.  
  31. // SetA4 is an inline for setting register a4 without using inline assembly.
  32. // it returns the old value of a4.
  33.  
  34. #pragma parameter __A0 SetA4(__A0)
  35. pascal void *SetA4(void *newA4) = 0xC948;
  36.  
  37. // iRelString is an interface to the RelString trap.  It returns a result such
  38. // that "iRelString(s1, s2) > 0" returns the same as "s1 > s2", that is, zero
  39. // if the strings are equal, 1 if s1 is greater that s2, -1 if neither.
  40.  
  41. #pragma parameter __D0 iRelString(__A0, __A1)
  42. pascal short iRelString(void *first, void *second) = {
  43.     0x7000,    // moveq    #0,d0
  44.     0x1018,    // move.b    (a0)+,d0
  45.     0x4840,    // swap        d0
  46.     0x1019,    // move.b    (a1)+,d0
  47.     0xA050    // RelString
  48. };
  49.  
  50. QDGlobals qd;
  51.  
  52. pascal void InitGame();
  53.  
  54. #define CurApNameTest     tst.b 0x910
  55.  
  56. //
  57. // QuickSilver.main.c
  58. //
  59. // Note: one concept was to patch HGetState etc. to the real minimum instructions required;
  60. //       need to investigate when and where that's possible.
  61. //
  62.  
  63. void realmain(Handle code);
  64.  
  65. pascal OSErr IconCatcher(OSType selector,long *response);
  66.  
  67.     // main has to be the first function in this file for our
  68.     // technique of setting up A4 to work.
  69. void main(void) {
  70.     void        *oldA4;
  71.     Handle        h;
  72.     long        codeLength;
  73.     long        i;
  74.  
  75.     if (keymap.shift) return;
  76.  
  77.     oldA4 = (void *)SetCurrentA4();    // set up access to Metrowerks C globals
  78.     h = Get1IndResource('INIT', 1);
  79.     codeLength = GetHandleSize(h);
  80.  
  81.     for (i = 0; i < codeLength; i += 2) {
  82.         if (*(short *)(*h + i) == 0x49F9) {
  83.             i += 2;
  84.             if (*(long *)(*h + i) == 0x23700732) {
  85.                 *(long *)(*h + i) = GetCurrentA4();
  86.             }
  87.         }
  88.     }
  89.     Flush68KCodeCaches();
  90.     
  91.     realmain(h);
  92.     InitGame();
  93.  
  94.     SetA4(oldA4);
  95. }
  96.  
  97. void *lastValidINITName;
  98.  
  99. static asm void pGet1IndRes(void) {
  100.         tst.l    0x914
  101.         ble.s    @oldGet1IndRes
  102.  
  103.         move.l    a4,-(a7)
  104.         lea        0x23700732,a4
  105.         move.l    0x914,lastValidINITName
  106.         move.l    (a7)+,a4
  107. @oldGet1IndRes:
  108. }
  109. static asm void oldGet1IndRes(void) {
  110.         jmp        0x12345678
  111. }
  112.  
  113. static long loaded_file_nums[1024];
  114.  
  115. static pascal void CloseAResFile(short refNum) {
  116.     Handle    h;
  117.     short    oldResLoad, oldROMMapInsert, oldResError;
  118.     short    ID;
  119.  
  120.     long    *rover;
  121.     long    filenum;
  122.     long    limit;
  123.  
  124.     if (*(long *)0x914 > 0) return; // only when INITs are being closed by the INIT loader
  125.  
  126.     filenum = *(long *)(*(long *)0x34E + refNum);
  127.     if (iRelString((void *)(*(long *)0x34E + refNum + 0x3E), lastValidINITName)) {
  128. //        *(void **)0x40 = (void *)(*(long *)0x34E + refNum + 0x3E);
  129. //        DebugStr("\pNot what we want??;dm 40^;dm 914^");
  130.         return;
  131.     }
  132.  
  133.     for (rover = loaded_file_nums; *rover;) {
  134.         if (*rover++ == filenum) return;
  135.     }
  136.     *rover = filenum;
  137.  
  138.     oldResError = *(short *)0xA60;
  139.  
  140.     UseResFile(refNum);
  141.  
  142.     oldResLoad = *(short *)0xA5E;
  143.     oldROMMapInsert = *(short *)0xB9E;
  144.     SetResLoad(1);
  145.     h = Get1IndResource('BNDL', 1);
  146.     if (h) {
  147.         rover = (long *)*h;
  148.         limit = GetHandleSize(h);
  149.         while (limit > 4) {
  150.             limit -= 2;
  151.             rover = (long *)(2 + (long)rover);
  152.             if (*rover == 'ICN#') {
  153.                 ID = *(short *)(8 + (long)rover);
  154.             //        ShowINIT(ID);
  155.                 Blit(ID);
  156.                 break;
  157.             }
  158.         }
  159.     }
  160.     *(short *)0xA5E = oldResLoad;
  161.     *(short *)0xB9E = oldROMMapInsert;
  162.  
  163.     *(short *)0xA60 = oldResError;
  164. }
  165.  
  166. // pascal void CloseResFile(short refNum)
  167.  
  168. static asm void pCloseResFile(void) {
  169.         tst.b    0x910
  170.         bgt.s    @oldCloseResFile
  171.         movem.l    d0-d2/a0-a1/a4,-(a7)
  172.         lea        0x23700732,a4
  173.         move.w    28(a7),-(a7)
  174.         jsr        CloseAResFile
  175.         movem.l    (a7)+,d0-d2/a0-a1/a4
  176. @oldCloseResFile:
  177. }
  178. static asm void oldCloseResFile(void) {
  179.         jmp        0x12345678
  180. }
  181.  
  182. // pascal void FillRect(Rect *r, Pattern *pat);
  183.  
  184. static asm pascal void pFillRect(Rect *r, Pattern *pat) {
  185.         move.l    a4,-(a7)
  186.         lea        0x23700732,a4
  187. //    if (*(char *)0x910 > 0 || *(char *)0x914 != -1) {
  188. //        oldFillRect(r, pat);
  189. //    }
  190.         tst.b    0x910
  191.         bgt.s    @doFill
  192.         cmpi.b    #-1,0x914
  193.         bne.s    @doFill
  194.         move.l    (a7)+,a4
  195.         move.l    (a7)+,(a7)
  196.         move.l    (a7)+,(a7)
  197.         rts
  198. @doFill:
  199.         move.l    (a7)+,a4
  200. }
  201. static asm void oldFillRect(void) {
  202.         jmp        0x12345678
  203. }
  204.  
  205. // pascal void FrameRect(Rect *r);
  206.  
  207. static asm pascal void pFrameRect(Rect *r) {
  208.         move.l    a4,-(a7)
  209.         lea        0x23700732,a4
  210. //    if (*(char *)0x910 > 0 || *(char *)0x914 != -1) {
  211. //        oldFillRect(r, pat);
  212. //    }
  213.         tst.b    0x910
  214.         bgt.s    @doFrame
  215.         cmpi.b    #-1,0x914
  216.         bne.s    @doFrame
  217.         move.l    (a7)+,a4
  218.         move.l    (a7)+,(a7)
  219.         rts
  220. @doFrame:
  221.         move.l    (a7)+,a4
  222. }
  223. static asm void oldFrameRect(void) {
  224.         jmp        0x12345678
  225. }
  226.  
  227. // pascal void CopyMask(BitMap *srcBits, BitMap *maskBits, BitMap *dstBits, Rect *srcRect, Rect *maskRect, Rect *dstRect)
  228.  
  229. static asm void pCopyMask(void) {
  230.         movem.l    d0/a0/a1/a4,-(a7)
  231.         lea        0x23700732,a4
  232.  
  233.         CurApNameTest
  234.         bgt        @not_what_we_want
  235.  
  236.         clr.l    -(a7)
  237.         pea        (a7)
  238.         dc.w    0xA870    //    _LocalToGlobal
  239.         tst.l    (a7)+
  240.         bne        @not_what_we_want
  241.  
  242.         move.l    20(a7),a0    // a0 = dstRect
  243.         move.w    4(a0),d0
  244.         sub.w    (a0),d0
  245.         cmpi.w    #32,d0
  246.         bne        @not_what_we_want
  247.         move.w    4(a0),d0
  248.         sub.w    (a0),d0
  249.         cmpi.w    #32,d0
  250.         bne        @not_what_we_want
  251.  
  252.         move.l    28(a7),a0    // a0 = srcRect
  253.         move.w    4(a0),d0
  254.         sub.w    (a0),d0
  255.         cmpi.w    #32,d0
  256.         bne        @not_what_we_want
  257.         move.w    4(a0),d0
  258.         sub.w    (a0),d0
  259.         cmpi.w    #32,d0
  260.         bne        @not_what_we_want
  261.  
  262.         lea        @junkRect,a0
  263.         move.l    a0,20(a7)    // replace dstRect with zero so the icon won't show up.
  264.         
  265. @not_what_we_want:
  266.         movem.l    (a7)+,d0/a0/a1/a4
  267.         bra.s    @oldCopyMask
  268. @junkRect:
  269.         dc.w    0,0,0,0
  270. @oldCopyMask:
  271. }
  272. static asm void oldCopyMask(void) {
  273.         jmp        0x12345678
  274. }
  275.  
  276. // pascal void CopyBits(BitMap *srcBits, BitMap *dstBits, Rect *srcRect, Rect *dstRect, short mode, RgnHandle maskRgn)
  277.  
  278. static asm void pCopyBits(void) {
  279.         movem.l    d0/a0/a1/a4,-(a7)
  280.         lea        0x23700732,a4
  281.  
  282.         CurApNameTest
  283.         bgt        @not_what_we_want
  284.  
  285.         clr.l    -(a7)
  286.         pea        (a7)
  287.         dc.w    0xA870    //    _LocalToGlobal
  288.         tst.l    (a7)+
  289.         bne        @not_what_we_want
  290.  
  291.         move.l    26(a7),a0    // a0 = dstRect
  292.         move.w    4(a0),d0
  293.         sub.w    (a0),d0
  294.         cmpi.w    #32,d0
  295.         bne        @not_what_we_want
  296.         move.w    4(a0),d0
  297.         sub.w    (a0),d0
  298.         cmpi.w    #32,d0
  299.         bne        @not_what_we_want
  300.  
  301.         move.l    30(a7),a0    // a0 = srcRect
  302.         move.w    4(a0),d0
  303.         sub.w    (a0),d0
  304.         cmpi.w    #32,d0
  305.         bne        @not_what_we_want
  306.         move.w    4(a0),d0
  307.         sub.w    (a0),d0
  308.         cmpi.w    #32,d0
  309.         bne        @not_what_we_want
  310.  
  311.         lea        @junkRect,a0
  312.         move.l    a0,26(a7)    // replace dstRect with zero so the icon won't show up.
  313.         
  314. @not_what_we_want:
  315.         movem.l    (a7)+,d0/a0/a1/a4
  316.         bra.s    @oldCopyBits
  317. @junkRect:
  318.         dc.w    0,0,0,0
  319. @oldCopyBits:
  320. }
  321. static asm void oldCopyBits(void) {
  322.         jmp        0x12345678
  323. }
  324.  
  325. // pascal void PlotIcon(Rect *theRect, Handle theIcon)
  326.  
  327. static asm void pPlotIcon(void) {
  328.         movem.l    d0/a0/a1/a4,-(a7)
  329.         lea        0x23700732,a4
  330.  
  331.         CurApNameTest
  332.         bgt        @not_what_we_want
  333.  
  334.         clr.l    -(a7)
  335.         pea        (a7)
  336.         dc.w    0xA870    //    _LocalToGlobal
  337.         tst.l    (a7)+
  338.         bne        @not_what_we_want
  339.  
  340.         move.l    24(a7),a0    // a0 = dstRect
  341.         move.w    4(a0),d0
  342.         sub.w    (a0),d0
  343.         cmpi.w    #32,d0
  344.         bne        @not_what_we_want
  345.         move.w    4(a0),d0
  346.         sub.w    (a0),d0
  347.         cmpi.w    #32,d0
  348.         bne        @not_what_we_want
  349.  
  350.         lea        @junkRect,a0
  351.         move.l    a0,24(a7)    // replace dstRect with zero so the icon won't show up.
  352.  
  353. @not_what_we_want:
  354.         movem.l    (a7)+,d0/a0/a1/a4
  355.         bra.s    @oldPlotIcon
  356. @junkRect:
  357.         dc.w    0,0,0,0
  358. @oldPlotIcon:
  359. }
  360. static asm void oldPlotIcon(void) {
  361.         jmp        0x12345678
  362. }
  363.  
  364. // pascal void PlotCIcon(Rect *theRect, CIconHandle theIcon)
  365.  
  366. static asm void pPlotCIcon(void) {
  367.         movem.l    d0/a0/a1/a4,-(a7)
  368.         lea        0x23700732,a4
  369.  
  370.         CurApNameTest
  371.         bgt        @not_what_we_want
  372.  
  373.         clr.l    -(a7)
  374.         pea        (a7)
  375.         dc.w    0xA870    //    _LocalToGlobal
  376.         tst.l    (a7)+
  377.         bne        @not_what_we_want
  378.  
  379.         move.l    24(a7),a0    // a0 = dstRect
  380.         move.w    4(a0),d0
  381.         sub.w    (a0),d0
  382.         cmpi.w    #32,d0
  383.         bne        @not_what_we_want
  384.         move.w    4(a0),d0
  385.         sub.w    (a0),d0
  386.         cmpi.w    #32,d0
  387.         bne        @not_what_we_want
  388.  
  389.         lea        @junkRect,a0
  390.         move.l    a0,24(a7)    // replace dstRect with zero so the icon won't show up.
  391.  
  392. @not_what_we_want:
  393.         movem.l    (a7)+,d0/a0/a1/a4
  394.         bra.s    @oldPlotCIcon
  395. @junkRect:
  396.         dc.w    0,0,0,0
  397. @oldPlotCIcon:
  398. }
  399. static asm void oldPlotCIcon(void) {
  400.         jmp        0x12345678
  401. }
  402.  
  403.  
  404.  
  405.  
  406. void realmain(Handle code) {
  407.     long        *oldTrap;
  408.  
  409. //    if (NewGestalt('IKch', &IconCatcher) != noErr) goto x9;
  410.  
  411.     // sanity checks here
  412.  
  413.     DetachResource(code);
  414.  
  415. #if 0
  416.     oldTrap = (long *)(2 + (long) &oldSyncWait);
  417.     *oldTrap = (long)GetOSTrapAddress(0xA0B7);
  418.     SetOSTrapAddress((void *)&pSyncWait, 0xA0B7);
  419. #endif
  420.  
  421.     oldTrap = (long *)(2 + (long) &oldGet1IndRes);
  422.     *oldTrap = (long)GetToolTrapAddress(0xA80E);
  423.     SetToolTrapAddress((void *) &pGet1IndRes, 0xA80E);
  424.  
  425.     oldTrap = (long *)(2 + (long) &oldCloseResFile);
  426.     *oldTrap = (long)GetToolTrapAddress(0xA99A);
  427.     SetToolTrapAddress((void *) &pCloseResFile, 0xA99A);
  428.  
  429.     oldTrap = (long *)(2 + (long) &oldFillRect);
  430.     *oldTrap = (long)GetToolTrapAddress(0xA8A5);
  431.     SetToolTrapAddress((void *) &pFillRect, 0xA8A5);
  432.  
  433.     oldTrap = (long *)(2 + (long) &oldFrameRect);
  434.     *oldTrap = (long)GetToolTrapAddress(0xA8A1);
  435.     SetToolTrapAddress((void *) &pFrameRect, 0xA8A1);
  436.  
  437.     oldTrap = (long *)(2 + (long) &oldCopyMask);
  438.     *oldTrap = (long)GetToolTrapAddress(0xA817);
  439.     SetToolTrapAddress((void *) &pCopyMask, 0xA817);
  440.  
  441.     oldTrap = (long *)(2 + (long) &oldCopyBits);
  442.     *oldTrap = (long)GetToolTrapAddress(0xA8EC);
  443.     SetToolTrapAddress((void *) &pCopyBits, 0xA8EC);
  444.  
  445.     oldTrap = (long *)(2 + (long) &oldPlotIcon);
  446.     *oldTrap = (long)GetToolTrapAddress(0xA94B);
  447.     SetToolTrapAddress((void *) &pPlotIcon, 0xA94B);
  448.  
  449.     oldTrap = (long *)(2 + (long) &oldPlotCIcon);
  450.     *oldTrap = (long)GetToolTrapAddress(0xAA1F);
  451.     SetToolTrapAddress((void *) &pPlotCIcon, 0xAA1F);
  452. }
  453.  
  454. #if 0
  455. pascal OSErr QuickSilver(OSType selector,long *_response) {
  456.     void    *oldA4 = SetA4(&main);
  457.     long    *response = _response;
  458.  
  459.     switch (selector) {
  460.         case 'QSlv':
  461.             *response = (long)&QuickSilver;
  462.         break;
  463.         case 'Pref':
  464.             *response = (long)&prefs;
  465.         break;
  466.         default:
  467.             SetA4(oldA4);
  468.             return gestaltUnknownErr;
  469.         break;
  470.     }
  471.  
  472.     SetA4(oldA4);
  473.     return noErr;
  474. }
  475. #endif
  476.  
  477.